{% include 'admin/header.html' %}
<script type="text/javascript">
    changeTab('学员管理');

    $(function () {
        $('.min-select').change(function () {
            var year = $(this).val().substring(0, 4);
            location.href = '/admin/student?year=' + year;
        });

        $('form.search').submit(function (e) {
            e.preventDefault();
            var name = $(this).find('input').val().trim();
            $.post('/by_name', {stu_name: name}, function (data) {
                if (data.status) {
                    var $tbody = $('table > tbody');
                    $tbody.html('');
                    $.each(data.result, function (k, a) {
                        var $tr = '<tr>\n' +
                            '\t\t\t\t\t\t\t\t<td>' + k + 1 + '</td>\n' +
                            '\t\t\t\t\t\t\t\t<td>' + a.stu_id + '</td>\n' +
                            '\t\t\t\t\t\t\t\t<td>' + a.stu_name + '</td>\n' +
                            '                                <td>' + a.stu_tel + '</td>\n' +
                            '\t\t\t\t\t\t\t\t<td>' + a.stu_class + '</td>\n' +
                            '\t\t\t\t\t\t\t\t<td>' + a.stu_reg_time.substring(0, 10) + '</td>\n' +
                            '                <td>' + a.record_num + '/' + a.class_num + '</td>\n' +
                            '                <td>' + a.work_num + '/' + a.class_num + '</td>\n' +
                            '\t\t\t\t\t\t\t\t<td>\n' +
                            '\t\t\t\t\t\t\t\t\t<button onclick="s_delete(' + a.id + ',' + '\'' + a.stu_name + '\''
                                            + ')" class="btn btn-xs btn-danger">删除</button>\n' +
                            '\t\t\t\t\t\t\t\t</td>\n' +
                            '\t\t\t\t\t\t\t</tr>';
                        $tbody.append($tr);
                    });
                }
            });
        });
    });

    function gotoPage(e, page) {
        var year = $('#year').val().substring(0, 4);
        var pages = {{ stu_list.pages }};
        var status = $(e).parent().attr("class");
        if (page > 0 && page <= pages && page !== {{ stu_list.page }} &&
            status !== "active") {
            location.href = '/admin/student?page=' + page + '&year=' + year;
        }
    }

    function s_delete(id, stu) {
        var flag = confirm("确定要删除<" + stu + ">吗？");
        if (flag) {
            $.post('/delete/' + id, function (data) {
                if (data.status) {
                    alert('SUCCESS: 删除成功');
                    location.reload();
                } else {
                    alert('ERROR: ' + data.msg);
                }
            });
        }
    }

    function Export_() {
        try {
            var year = $('#year').val().substring(0, 4);
            window.open('/export?year=' + year, '_self');
        } catch (err) {
            alert("无任何数据！");
            console.log(err);
        }
    }
    $(function () {
        $('#Import_').click(function (e) {
            $('#import_stu').click();
        });

        $('#import_stu').change(function (e) {
            var file = e.target.files[0];
            var fd = new FormData();
            fd.append('student', file);
            var xhr = new XMLHttpRequest();
            xhr.open('post', '/import', true);
            xhr.onload = function (e) {
                var data = JSON.parse(e.target.responseText);
                if (data.status) {
                    alert("导入成功!");
                    location.reload();
                } else {
                    alert("导入失败: " + data.msg);
                }
            };
            xhr.send(fd);
        });
    });
</script>
<div class="work-xcontent">
    <div>
        <div class="col-lg-12 col-xs-12">
            <h4><span class="fa fa-hand-o-right"></span> 学生管理</h4>
            <div style="padding: 15px 5px 0;" class="pull-right">
                <div class="pull-left">
                    <div class="btn-group-sm">
                        <button onclick="Export_()" class="btn btn-success">导出</button>
                        <input id="import_stu" type="file" style="display: none;" name="student" accept="application/vnd.ms-excel">
                        <button id="Import_" class="btn btn-warning">导入</button>
                    </div>
                </div>
                <div class="input-group min-input pull-left">
                    <select id="year" class="form-control min-select">
                        {% for foo in years %}
                            {% if loop.index > 1 %}
                                <option {% if years[0] == foo.stu_reg_time %}selected{% endif %}>
                                    {{ foo.stu_reg_time }}年度
                                </option>
                            {% endif %}
                        {% endfor %}
                    </select>
                </div>
                <form class="search pull-right">
                    <div class="input-group min-input">
                        <input name="username" style="height: 30px;" class="form-control" placeholder="姓名">
                        <span class="input-group-btn">
                            <button type="submit" class="btn btn-default btn-sm">
                                <span class="fa fa-search"></span>
                            </button>
                        </span>
                    </div>
                </form>
            </div>
        </div>
        <div>
            <div class="col-lg-12 col-xs-12">
                <table class="table table-striped">
                    <thead class="bg-color-x">
                    <tr>
                        <td>索引</td>
                        <td>账号</td>
                        <td>名字</td>
                        <td>电话</td>
                        <td>班级</td>
                        <td>注册日期</td>
                        <td>出勤比例</td>
                        <td>作业比例</td>
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody>
                    {% if not stu_list or stu_list.total == 0 %}
                        <tr>
                            <td align="center" colspan="9">暂无数据</td>
                        </tr>
                    {% else %}
                        {% for a in stu_list.result %}
                            <tr>
                                <td>{{ loop.index }}</td>
                                <td>{{ a.stu_id }}</td>
                                <td>{{ a.stu_name }}</td>
                                <td>{{ a.stu_tel }}</td>
                                <td>{{ a.stu_class }}</td>
                                <td>{{ a.stu_reg_time[:10] }}</td>
                                <td>{{ a.record_num }}/{{ a.class_num }}</td>
                                <td>{{ a.work_num }}/{{ a.class_num }}</td>
                                <td>
                                    <button onclick="s_delete({{ a.id }}, '{{ a.stu_name }}')"
                                            class="btn btn-xs btn-danger">删除
                                    </button>
                                </td>
                            </tr>
                        {% endfor %}
                    {% endif %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <!-- 分页按钮 -->
    {% if stu_list.pages > 1 %}
        <div class="col-lg-12 col-xs-12 text-center">
            <ul style="margin: 0;" class="pagination pagination-sm">
                <li onclick="gotoPage(this, 1)"><a href="javascript:">首页</a></li>
                <li onclick="gotoPage(this, {{ stu_list.page - 1 }})"><a href="javascript:">&laquo;</a></li>
                {% for foo in stu_list.pageNums %}
                    <li onclick="gotoPage(this, {{ foo }})"
                        class="{% if foo == stu_list.page %}active{% endif %}">
                        <a href="javascript:">{{ foo }}</a>
                    </li>
                {% endfor %}
                <li onclick="gotoPage(this, {{ stu_list.page + 1 }})"><a href="javascript:">&raquo;</a></li>
                <li onclick="gotoPage(this, {{ stu_list.pages }})"><a href="javascript:">尾页</a></li>
            </ul>
            <span style="padding-top: 15px; font-size: 12px;" class="pull-right">
                共{{ stu_list.pages }}页，{{ stu_list.total }}条数据
            </span>
        </div>
    {% endif %}
</div>

{% include 'admin/footer.html' %}